﻿using System;
using Algoritmia.Utils;
using Lists = Algoritmia.DataStructures.Lists;

namespace Algoritmia.Problems.Sorting
{
    public class InPlaceBubbleSorter<T> : IInPlaceSorter<T>
    {
        #region IInPlaceSorter<T> Members

        public void Sort(Lists.IList<T> list, Comparison<T> comparison = null)
        {
            comparison = AlgoritmiaFactory.GetDefaultComparison(comparison);

            for(var i = 0; i < list.Count - 1; i++)
            {
                for(var j = 0; j < list.Count-1-i; j++)
                {
                    if (comparison(list[j],list[j+1]) > 0) 
                    {
                        T tmp = list[j];
                        list[j] = list[j + 1];
                        list[j + 1] = tmp;
                    }
                }
            }
        }
        #endregion
    }
}
